package com.hansen.library.utils;

import android.util.SparseArray;

import com.hansen.library.Constants;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by han on 2018/10/15 0015.
 */

public class CommonUtils {

    /**
     * 最大显示未读数
     */
    public static final int MAX_UNREAD_SHOW_NUMBER = 99;

    public static boolean isEmptyList(List mList) {
        return mList == null || mList.size() <= 0;
    }

    public static int getListSize(List mList) {
        return mList == null ? 0 : mList.size();
    }

    public static int getListSize(SparseArray mList) {
        return mList == null ? 0 : mList.size();
    }

    public static boolean isArrayIndexOutOfBounds(SparseArray mList, int pos) {
        return (mList == null || pos < 0 || pos >= mList.size());
    }

    public static boolean isArrayIndexOutOfBounds(List mList, int pos) {
        return (mList == null || pos < 0 || pos >= mList.size());
    }


    public static boolean isArrayIndexOutOfBounds(String[] mArray, int pos) {
        return (mArray == null || pos < 0 || pos >= mArray.length);
    }

    public static boolean isMapIndexOutOfBounds(Map maps, int pos) {
        return (maps == null || pos < 0 || pos >= maps.size());
    }

    public static String unreadMessageShowRule(int unread) {
        return unread > 99 ? "99+" : String.valueOf(unread);
    }

    public static boolean isHasMoreData(List mList) {
        return (mList != null && mList.size() >= Constants.PAGE_SIZE_INT);
    }

    public static String getSexByCode(String code) {
        if(Constants.SEX_GIRL.equals(code)) {
            return "女";
        } else if(Constants.SEX_BOY.equals(code)) {
            return "男";
        }

        return "";
    }

    /**
     * 使用 Map按key进行排序
     *
     * @param map
     * @return
     */
    public static Map<String, String> sortMapByKey(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Map<String, String> sortMap = new TreeMap<String, String>(
                new Comparator<String>() {
                    @Override
                    public int compare(String str1, String str2) {
                        return str1.compareTo(str2);
                    }
                });

        sortMap.putAll(map);

        return sortMap;
    }
}
